package com.hltx.controller.goods;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hltx.constants.GoodsConstant;
import com.hltx.controller.base.BaseController;
import com.hltx.pojo.common.Page;
import com.hltx.pojo.ratioconfigure.RatioConfigure;
import com.hltx.resolver.DefiniteException;
import com.hltx.service.goods.GoodsManager;
import com.hltx.service.pay.PayManager;
import com.hltx.util.CustomUtil;
import com.hltx.util.DataUtil;
import com.hltx.util.DateUtil;
import com.hltx.util.ResultInfo;





/**
 * 商品controller
 * 
 * @author shibeilei
 *
 */
@Controller
@RequestMapping("/goods")
public class GoodsController extends BaseController{
	
	@Autowired
	private GoodsManager goodsService;
	
	@Autowired
	private PayManager payService;

	/**
	 * 打开商品列表
	 */
	@RequestMapping
	public ModelAndView index(HttpServletRequest request) {
		String parentid=request.getParameter("parentid");
		String search = CustomUtil.strTransCode(request.getParameter("search"));
		String classifyname =  CustomUtil.strTransCode(request.getParameter("classifyname"));
		String classid =request.getParameter("classid");
		if(parentid == null){parentid = "goods";}
		if(!(search == null || "".equals(search))){parentid = "search";}			
		ModelAndView view  = this.getModelAndView();
		view.addObject("parentid", parentid);
		view.addObject("classid",classid);
		view.addObject("search", search);
		view.addObject("classifyname", classifyname);
		view.setViewName("goods/goods_list");
		return view;
	}
	
	
	@RequestMapping(value = "/getHotGoodsList", method = { RequestMethod.POST})
	@ResponseBody
	public Map<String,Object> getHotGoodsList(Page p, HttpServletRequest request, HttpSession session){
		this.logBefore(logger, "查询热门商品列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			List<Map<String, Object>> hotGoodsList = goodsService.getHotGoodsList(p);
			resMap.put("rHotGoodsList", hotGoodsList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	@RequestMapping(value = "/getGoodsList", method = { RequestMethod.POST})
	@ResponseBody
	public Map<String,Object> getGoodsList(Page p, HttpServletRequest request, HttpSession session){
		this.logBefore(logger, "查询商品列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			List<Map<String, Object>> goodsList = goodsService.getGoodsList(p);
			resMap.put("rGoodsList", goodsList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}	
	
	/**
	 * 打开商品详情
	 */
	@RequestMapping("/detailPage")
	public ModelAndView detail(String goodsId) {
		ModelAndView view = this.getModelAndView();
		if(goodsId == null || goodsId.length() ==0){
			throw new DefiniteException("参数不正确！");
		}else{
			view.addObject("id", goodsId);
			view.setViewName("goods/goods_detail");
		}
		return view;
	}
	
	/**
	 * 获取商品详细信息
	 */
	@RequestMapping(value = "/detail", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object> detail(Map<String, String> params, HttpServletRequest request, HttpSession session) {
		this.logBefore(logger, "查询商品详情");
		params.put("id", request.getParameter("id"));
		Map<String, Object> resultMap  = new HashMap<>();
		Map<String, Object> rParams = new HashMap<String, Object>();
		rParams.put("goodsId", request.getParameter("id"));
		rParams.put("curTime", DateUtil.getTime());
		try{
			List<Map<String, Object>> list = goodsService.getGoodsDetail(params);
			if(list.size() <= 0){
				resultMap.put("result", this.initResult(ResultInfo.ERROR, "error", GoodsConstant.GOOD_NO_FIND));
			}else{
				Map<String, Object> listMap = list.get(0);
				String isSelectSpec = (String)list.get(0).get("is_select_spec");
				Map<String, Object> goodsSales = goodsService.getGoodsSalesByGId(rParams);
				RatioConfigure ratioConfigure = payService.getRatioConfigure();
				if(goodsSales == null){
					list.get(0).put("is_sales", "00");
				}
				BigDecimal bidPrice = (BigDecimal) listMap.get("bid_price");
				BigDecimal marketPrice = (BigDecimal) listMap.get("market_price");
				BigDecimal price = (BigDecimal) listMap.get("price");
				if("01".equals(list.get(0).get("is_sales"))){
					price = (BigDecimal) listMap.get("real_price");
				}
				BigDecimal discount = bidPrice.multiply(DataUtil.getBigDecimal(10)).divide(marketPrice, 2 , BigDecimal.ROUND_HALF_DOWN);
				
				BigDecimal minRatio = ratioConfigure.getMinRatio();
				BigDecimal maxRatio = ratioConfigure.getMaxRatio();
				if(discount.compareTo(maxRatio) <= 0){
					int changePoints =0;
					if (discount.compareTo(minRatio) == 1) {
						 changePoints = CustomUtil.requiredIntegral(DataUtil.getBigDecimal(price, 2), discount, minRatio);
					}else{
						 changePoints = price.intValue();
					}
					BigDecimal changePrice = price.subtract(DataUtil.getBigDecimal(changePoints, 2));
					listMap.put("change_points", changePoints);
					listMap.put("change_price", changePrice);
					listMap.put("real_price", price);
				}
				if("01".equals(isSelectSpec)){
					List<Map<String, Object>> rSpecType = goodsService.getSpecType(params);
					Map<String, Map<String, Object>> goodsSpecList = goodsService.getGoodsAllSpec(params);
					for(Map<String,Object> o :rSpecType){
						params.put("lableId", o.get("id").toString());
						List<Map<String,Object>> rSpecDetail = goodsService.getSpecDetail(params);
						o.put("rSpecDetail", rSpecDetail);
					}
					resultMap.put("rSpecType", rSpecType);
					resultMap.put("goodsSpecList", goodsSpecList);
					}
				resultMap.put("ratioConfigure", ratioConfigure);
				resultMap.put("obj", list.get(0));
				resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
			}
			
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resultMap;
	}
}
